<template>
	<view class="addWrapper">
		<view class="options">
			<button @tap="Add_GoodsUnitMessage">{{isChange?'修改':'新增'}}</button>
			<button @tap="clear()">清空</button>
		</view>
		<view class="cu-form-group">
			<view class="title">单位：</view>
			<view class="inpt1"><input type="text" placeholder="单击选择" v-model="NowSelectGood.UnitName" style="text-align: left;"
				 data-target="UnitNameModal" @tap="showModal" disabled/>
				<view class="cu-modal" :class="modalName=='UnitNameModal'?'show':''">
					<view class="cu-dialog">
						<view class="cu-bar bg-white justify-end">
							<view class="content">请选择单位</view>
							<view class="action" @tap="hideModal">
								<text class="cuIcon-close text-red"></text>
							</view>
						</view>
						<view class="padding-xl">
							<view class="searchStore">
								<input placeholder="请输入名称" v-model="SearchUnitName" />
								<button @tap="searchUnitName">搜索</button>
							</view>
							<view class="StoreListWrap">
								<view v-for="(item,index) in UnitNameList" :key="index" @tap="selectUnitName(index)">{{item.UnitName}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cu-form-group">
			<view class="title">*比率:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.URate" type="numbe"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">进价:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.InPrice" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">零售价:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.SalePrice" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">单价1:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.Price1" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">单价2:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.Price2" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">配送价:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.Price3" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">会员价:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.VipPrice" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">批发价:</view>
			<input class="inpt" name="input" v-model="NowSelectGood.PFPrice" type="number"></input>
		</view>
	</view>
</template>
<script>
	import * as Api from '@/syApis/syDa/syDa.js';
	import * as Apis from '@/syApis/syAddOrder/syaddApis.js';
	export default {
		data() {
			return {
				//多单位列表
				UnitNameList: [],
				//商品ID
				GoodsID: "",
				NowSelectGood: {
					URate: "1",
					UnitID:"",
					InPrice: "0",
					SalePrice: "0",
					Price1: "0",
					Price2: "0",
					Price3: "0",
					VipPrice: "0",
					PFPrice: "0",
					UnitName: ""
				},
                modalName:null,
				SearchUnitName:"",
				isChange:false
				}
		},
		onLoad(options) {
			if(options.type!=0){
				this.isChange = false;
				uni.setNavigationBarTitle({
					title:"新增商品多单位"
				})
			}else{
				this.isChange = true;
				this.NowSelectGood = JSON.parse(options.UnitItem);
				this.NowSelectGood.URate = this.NowSelectGood.urate;
				uni.setNavigationBarTitle({
					title:"修改商品多单位"
				})
			}
			this.GoodsID = options.GoodsID;
			this.Get_GoodsUnitMessage();
		},
		methods: {
			Add_GoodsUnitMessage(){
				if(this.isChange){
					this.Update_GoodsUnitMessage();
					return;
				}
				if(this.NowSelectGood.UnitName==""){
					Api.showTip("单位不能为空");
					return;
				}
				var that = this;
				this.NowSelectGood.GoodsID = this.GoodsID;
				this.NowSelectGood.CreateID = uni.getStorageSync("UserCode");
				let data = {
					Save_M_Data_Json:JSON.stringify(this.NowSelectGood)
				}
				Api.Add_GoodsUnitMessage(data,function(res){
					  if(res.state){
						  if(res.data.ErrorMsg!=""){
							  Api.showTip(res.data.ErrorMsg);
						  }else if(res.data.ErrorMsg=='获取登录信息失败'){
							  Api.showTip("登录超时");
						  }else{
							   Api.showTip("新增成功");
							   that.$eventHub.$emit("fires",null);
						  }
					  }
				});
			},
			Update_GoodsUnitMessage(){
				if(this.NowSelectGood.UnitName==""){
					Api.showTip("单位不能为空");
					return;
				}
				var that = this;
				this.NowSelectGood.GoodsID = this.GoodsID;
				// this.NowSelectGood.GoodsUnitID = this.NowSelectGood.GoodsUnitID;
				this.NowSelectGood.CreateID = uni.getStorageSync("UserCode");
				let data = {
					Save_M_Data_Json:JSON.stringify(this.NowSelectGood)
				}
				Api.Update_GoodsUnitMessage(data,function(res){
					  if(res.state){
						  if(res.data.ErrorMsg!=""){
							  Api.showTip(res.data.ErrorMsg);
						  }else if(res.data.ErrorMsg=='获取登录信息失败'){
							  Api.showTip("登录超时");
						  }else{
							   Api.showTip("修改成功");
							   that.$eventHub.$emit("fires",null);
							   setTimeout(function() {
								   uni.navigateBack({
								   	
								   })
							   }, 200);
						  }
					  }
				});
			},
			clear(){
				this.NowSelectGood= {
					URate: "1",
					InPrice: "0",
					SalePrice: "0",
					Price1: "0",
					Price2: "0",
					Price3: "0",
					VipPrice: "0",
					PFPrice: "0",
					UnitName: ""
				}
			},
			showModal(e){
				this.modalName = e.currentTarget.dataset.target;
			},
			hideModal(){
				this.modalName = null;
			},
			//多单位点击
			selectUnitName(index){
				 this.NowSelectGood.UnitName = this.UnitNameList[index].UnitName;
				 this.NowSelectGood.UnitID = this.UnitNameList[index].UnitID;
				 // this.NowSelectGoods.urate = this.UnitNameList[index].urate;
				 // this.NowSelectGoods.UnitID = this.UnitNameList[index].UnitID;
				 this.modalName = null;
			},
			//查询商品多单位
			Get_GoodsUnitMessage(Filter = ''){
				let data = {
					GoodsID: this.GoodsID,
					Filter: Filter
				}
				var that = this;
				Api.Get_GoodsUnitMessage(data, function(res) {
					if (res.state) {
						if (res.data.ResultMsg != "") {
							that.UnitNameList = res.data.ResultMsg;
						} else if (res.data.ErrorMsg == '获取登录信息失败') {
							Api.showTip(res.data.ErrorMsg);
						} else {
							Api.showTip(res.data.ErrorMsg);
						}
					} else {

					}
				});
			},
		}
	}
</script>

<style lang="css">
.inpt1 {
		flex: 1;
		text-align: left;
		align-items: center;
		display: flex;
	}
	.options{
		width: 100%;
		height: 40px;
		display: flex;
	}
	.options button{
		width: 45%;
		height: 35px;
		line-height: 35px;
		margin-top: 2.5px;
		color: #FFF;
		font-size: 14px;
		margin: auto;
		
		background: rgba(124, 170, 216,1);
	}
	
	.stop {
		background: rgba(124, 170, 216,1);
	}
	.inpt1 {
		flex: 1;
		text-align: left;
		align-items: center;
		display: flex;
	}
	/* 弹框样式 */
	.padding-xl .searchStore{
		display: flex;
		width: 100%;
		margin: auto;
		height: 45px;
		padding-top: 5px;
		font-size: 14px;
	}
	.padding-xl .searchStore input{
		flex: 1;
		height: 35px;
		border: 1px solid #CCC;
		border-top-left-radius: 5px;
		border-bottom-left-radius: 5px;
		padding-left: 10px;
	}
	.padding-xl{
		padding: 10px;
		
	}
	.padding-xl .searchStore button{
		font-size: 14px;
		color:#FFF;
		padding-left: 8px;
		padding-right: 8px;
		height: 35px;
		line-height: 35px;
		border-radius: 0px;
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
		text-align: center;
		background: rgba(124, 170, 216, 1);
	}
	.padding-xl .StoreListWrap{
		height: 250px;
		overflow: auto;
	}
	.padding-xl .StoreListWrap view{
		height: 40px;
		line-height: 40px;
		text-align: left;
		border-bottom: 1px solid #CCC;
		padding-left: 8px;
	}
	.cu-form-group{
		padding: 1px 10px !important;
		height: 40px;
		line-height: 40px;
		min-height: 40px;
	}
</style>
